/**
 *  @win8.js
 *  @version: 1.00
 *  @author: Jesse Freeman
 *  @date: May 2012
 *  @copyright (c) 2012 Jesse Freeman, under The MIT License (see LICENSE)
 *
 */

ig.module(
    'bootstrap.platforms.win8'
)
    .requires(
    'impact.input',
    'impact.game'
)

    .defines(function ()
    {

        //TODO need to set up logic for resizing the game correctly for win8, showing touch controls and winjs specific logic
        var sizes = {
            small: { width: 240, height: 160, scale: 2 },
            medium: { width: 240, height: 160, scale: 2 },
            large: { width: 240, height: 160, scale: 4 },
            default: { width: 240, height: 155, scale: 5 }
        };

        ig.gameSize = sizes.default;
        
        if (typeof(WinJS) != 'undefined') {
            var app = WinJS.Application;
            var activation = Windows.ApplicationModel.Activation;

            app.onactivated = function (args) {
                if (args.detail.kind === activation.ActivationKind.launch) {
                    if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                        // TODO: This application has been newly launched. Initialize
                        // your application here.
                        ig.startNewGame();

                        window.addEventListener("resize", onViewStateChanged);
                    } else {
                        // TODO: This application has been reactivated from suspension.
                        // Restore application state here.
                    }
                    args.setPromise(WinJS.UI.processAll());
                }
            };

            function onViewStateChanged(event) {
                var viewStates = Windows.UI.ViewManagement.ApplicationViewState;
                var newViewState = Windows.UI.ViewManagement.ApplicationView.value;

                //TODO need to tell if we are in game over state or pause state when we return from a snap

                var scale = 1; //Math.floor(window.innerHeight / 155);
                var maxWidth = window.innerWidth;
                var maxHeight = window.innerHeight;
                var width = Math.floor(maxWidth / scale) - 1;
                var height = Math.floor(maxHeight / scale) - 1;

                var controls = document.getElementById("controls");

                ig.system.resize(width, height, scale);

                //TODO need to test for touch
                if (newViewState === viewStates.snapped) {
                    // is snapped
                    controls.style.visibility = "hidden";

                } else if (newViewState === viewStates.filled) {
                    // is filled
                    //ig.system.resize(width, height, scale);
                    controls.style.visibility = "visible";
                } else if (newViewState === viewStates.fullScreenLandscape) {
                    // is full screen
                    controls.style.visibility = "visible";
                }

                if (ig.game.camera) {
                    ig.game.configureCamera();
                    ig.game.camera.update();
                }
                ig.game.togglePause(true);
            }


            ig.Input.inject({
                bindTouch: function (selector, action) {
                    var element = ig.$(selector);

                    var that = this;
                    element.addEventListener('MSPointerDown', function (ev) {
                        that.touchStart(ev, action);
                    }, false);

                    element.addEventListener('MSPointerUp', function (ev) {
                        that.touchEnd(ev, action);
                    }, false);
                },
            });

            WinJS.Application.onsettings = function (e) {
                ig.game.togglePause(true);
                //Example adding help to the win8 charms
                //          e.detail.applicationcommands = { "help": { title: "Help", href: "/html/help.html" } };
                //          WinJS.UI.SettingsFlyout.populateSettings(e);

            };

            ig.Debug.inject({
                active: true,
                toggleButton: null,
                ready: function () {
                    this.parent();

                    if (!this.toggleButton) {
                        this.toggleButton = document.getElementById("cmdDebugger");
                        var that = this;
                        this.toggleButton.addEventListener("click", function () { that.toggle(); }, false);

                        this.refreshToggleButtonLabel();
                    }

                },
                toggle: function () {

                    this.active = !this.active;
                    this.container.style.display = this.active ? 'block' : 'none';

                    this.refreshToggleButtonLabel();
                },
                refreshToggleButtonLabel: function () {
                    document.getElementById("cmdDebugger").winControl.label = (this.active ? "Hide" : "Show") + " Debug";
                }

            });
        }

    });


